package org.dolphin.action.misc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.dolphin.base.bean.DueDatePeriodVO;
import org.dolphin.base.constant.Status;
import org.dolphin.base.json.PaginationBean;
import org.dolphin.base.json.StatusBean;
import org.dolphin.entity.misc.DueDatePeriod;
import org.dolphin.service.misc.DueDatePeriodService;
import org.dolphin.util.ExceptionHelper;
import org.dolphin.util.Pagination;
import org.hibernate.criterion.Order;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "custom-default")
@Namespace(value = "/misc/period/dueDate")
@InterceptorRefs(value = {@InterceptorRef(value = "customStack")})
public class DueDatePeriodAction extends ActionSupport {
  private static final long serialVersionUID = 5308721573027674410L;
  private Integer page;
  private Integer rows;
  private String sidx;
  private String sord;
  private StatusBean status;
  private DueDatePeriodService dueDatePeriodService;
  private DueDatePeriodVO dueDatePeriod;
  private PaginationBean pagination;

  @Action(value = "addDueDatePeriod", results = @Result(name = "success", location = "addDueDatePeriod.jsp"))
  public String addDueDatePeriod() {
    return SUCCESS;
  }

  @Action(value = "createDueDatePeriod", results = @Result(name = "success", params = {"root",
      "status"}, type = "json"))
  public String createDueDatePeriod() {
    boolean error = false;
    String msg = "";
    status = new StatusBean();
    try {
      DueDatePeriod period = new DueDatePeriod();
      period.setCode(dueDatePeriod.getCode());
      period.setYear(dueDatePeriod.getYear());
      period.setMonth(dueDatePeriod.getMonth());
      period.setWeek(dueDatePeriod.getWeek());
      period.setDay(dueDatePeriod.getDay());
      getDueDatePeriodService().createDueDatePeriod(period);
    } catch (Exception e) {
      error = true;
      msg = ExceptionHelper.getRootCause(e).getMessage();
    } finally {
      status.setStatus(error ? Status.ERROR : Status.SUCCESS);
      if (error && msg != null) {
        status.addError(msg);
      }
    }
    return SUCCESS;
  }

  public DueDatePeriodVO getDueDatePeriod() {
    return dueDatePeriod;
  }

  @Action(value = "getDueDatePeriods", results = @Result(name = "success", params = {"root",
      "pagination"}, type = "json"))
  public String getDueDatePeriods() {
    List<Order> orders = null;
    if (StringUtils.hasText(getSidx())) {
      orders = new ArrayList<Order>(1);
      Order order = ObjectUtils.nullSafeEquals("desc", getSord()) ? Order.desc(getSidx()) : Order
          .asc(getSidx());
      orders.add(order);
    }
    List<DueDatePeriodVO> results = getDueDatePeriodService().getDueDatePeriods(page, rows, orders);
    Long records = getDueDatePeriodService().getDueDatePeriodsCount();
    Integer total = Pagination.calculateTotalPage(records, getRows());
    pagination = new PaginationBean();
    pagination.setPage(getPage());
    pagination.setRecords(records);
    pagination.setTotal(total);
    pagination.setRows(results);
    return SUCCESS;
  }

  public DueDatePeriodService getDueDatePeriodService() {
    return dueDatePeriodService;
  }

  public Integer getPage() {
    return page;
  }

  public PaginationBean getPagination() {
    return pagination;
  }

  public Integer getRows() {
    return rows;
  }

  public String getSidx() {
    return sidx;
  }

  public String getSord() {
    return sord;
  }

  public StatusBean getStatus() {
    return status;
  }

  @Action(value = "listDueDatePeriod", results = @Result(name = "success", location = "listDueDatePeriod.jsp"))
  public String listDueDatePeriod() throws IOException {
    return SUCCESS;
  }

  public void setDueDatePeriod(DueDatePeriodVO dueDatePeriod) {
    this.dueDatePeriod = dueDatePeriod;
  }

  public void setDueDatePeriodService(DueDatePeriodService dueDatePeriodService) {
    this.dueDatePeriodService = dueDatePeriodService;
  }

  public void setPage(Integer page) {
    this.page = page;
  }

  public void setPagination(PaginationBean pagination) {
    this.pagination = pagination;
  }

  public void setRows(Integer rows) {
    this.rows = rows;
  }

  public void setSidx(String sidx) {
    this.sidx = sidx;
  }

  public void setSord(String sord) {
    this.sord = sord;
  }

  public void setStatus(StatusBean status) {
    this.status = status;
  }
}
